package controllers;

import java.util.List;

import com.google.gson.JsonObject;

import models.ActionItem;
import models.ActionPlan;
import models.Idea;
import models.IdeaDeveloper;
import models.Tag;
import models.Topic;
import play.mvc.Controller;

public class TagPage extends Controller {

	/**
	 * This method renders the TagView page and passing the id and the name of
	 * the tag
	 */
	public static void TagView(String name) {
		Tag tag = Tag.find("name=?", name).first();
		String thisTagId = tag.id + "";
		String thisTagName = tag.name;
		String creator = "";
		try {
			creator = "<a href=\"http://localhost:9000/UserProfile/OtherUserProfile?userId="
					+ tag.creator.id + "\">" + tag.creator.username + "</a>";
		} catch (Exception e) {
			creator = "<a href='#'>Creator</a>";
		}
		render(thisTagId, thisTagName, creator);
	}

	/**
	 * Rendering a table of ideas in a form of
	 * <table>
	 * in HTML
	 * 
	 * @param tagId
	 *            the id of the tag
	 */
	public static void loadTagIdeas(long tagId) {
		Tag thisTag = Tag.find("id=?", tagId).first();
		List<Idea> ideas = thisTag.ideas;
		String result = "<table id=\"ideaTable\" border=\"1\">";
		for (Idea idea : ideas) {
			result += "<tr><td><a href=\"http://localhost:9000/browsecontroller/browse\" >"
					+ idea.title + "</a></td>";
			result += "<td>" + idea.description + "</td></tr>";
		}
		
		result += "</table>";
		JsonObject json = new JsonObject();
		json.addProperty("result", result);
		renderJSON(json.toString());
	}

	/**
	 * Rendering a table of topics in a form of
	 * <table>
	 * in HTML
	 * 
	 * @param tagId
	 *            the id of the tag
	 */
	public static void loadTagTopics(long tagId) {
		Tag thisTag = Tag.find("id=?", tagId).first();
		List<Topic> topics = thisTag.topics;
		String result = "<table id=\"ideaTable\" border=\"1\">";
		for (Topic topic : topics) {
			result += "<tr><td><a href=\"http://localhost:9000/browsecontroller/browse\" >"
					+ topic.title + "</a></td>";
			result += "<td>" + topic.description + "</td></tr>";
		}
		result += "</table>";
		JsonObject json = new JsonObject();
		json.addProperty("result", result);
		renderJSON(json.toString());
	}

	/**
	 * Rendering a table of action items in a form of
	 * <table>
	 * in HTML
	 * 
	 * @param tagId
	 *            the id of the tag
	 */
	public static void loadTagActionItems(long tagId) {
		Tag thisTag = Tag.find("id=?", tagId).first();
		List<ActionItem> actionItems = thisTag.actionitems;
		String result = "<table id=\"ideaTable\" border=\"1\">";
		for (ActionItem actionItem : actionItems) {
			result += "<tr><td><a href=\"http://localhost:9000/browsecontroller/browse\" >"
					+ actionItem.body + "</a></td></tr>";
		}
		result += "</table>";
		JsonObject json = new JsonObject();
		json.addProperty("result", result);
		renderJSON(json.toString());
	}

	/**
	 * checks if the user is already a follower for this tag or not
	 * 
	 * @author nataly.wadie
	 * @param tagId
	 *            id of the tag that the idea developer wants to follow
	 */
	public static void canFollowTag(long tagId) { // tag 1
		Tag tag = (Tag) Tag.findById(tagId);
		String followerName = Security.connected();
		IdeaDeveloper follower = (IdeaDeveloper) IdeaDeveloper
				.find("username=?", followerName).fetch().get(0);
		JsonObject tagCheck = new JsonObject();
		if (!Tag.followCheck(follower, tag)) {
			tagCheck.addProperty("followTag", "can follow");
			renderJSON(tagCheck.toString());
		}
		if (Tag.followCheck(follower, tag)) {
			tagCheck.addProperty("followTag", "you are already a follower");
			renderJSON(tagCheck.toString());
		}
	}

	/**
	 * makes sure that the user is not a follower to this tag and adds him to
	 * the list of followers of this tag and adds the tag to the list of
	 * followed tags of this user (via the followTag method in the Tag model)
	 * 
	 * @author nataly.wadie
	 * @param tagId
	 *            id of tag that the idea developer wants to follow
	 */
	public static void followTag(long tagId) { // tag2
		String followerName = Security.connected();
		IdeaDeveloper follower = (IdeaDeveloper) IdeaDeveloper
				.find("username=?", followerName).fetch().get(0);
		Tag tag = (Tag) Tag.findById(tagId);
		JsonObject followTag = new JsonObject();
		if (!Tag.followCheck(follower, tag)) {
			Tag.followTag(follower, tag);
		}
		followTag.addProperty("followTag", "successfully followed");
		renderJSON(followTag.toString());
	}

	/**
	 * makes sure that the user is already a follower to this tag and removes
	 * him from the list of followers of the tag and removes the tag from the
	 * list of followed tags of this user(via the method unfollowTag in the Tag
	 * model)
	 * 
	 * @author nataly.wadie
	 * @param tagId
	 *            id of the tag that is going to be unfollowed by the user
	 */
	public static void unfollowTag(long tagId) { // tag 3
		String unfollowerName = Security.connected();
		IdeaDeveloper unfollower = (IdeaDeveloper) IdeaDeveloper
				.find("username=?", unfollowerName).fetch().get(0);
		Tag tag = (Tag) Tag.findById(tagId);
		JsonObject unfollowTag = new JsonObject();
		if (Tag.followCheck(unfollower, tag)) {
			Tag.unfollowTag(unfollower, tag);
		}
		unfollowTag.addProperty("unfollowTag", "successfully unfollowed");
		renderJSON(unfollowTag.toString());
	}

}
